package com.bbx.taxi.client.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import butterknife.Bind;

import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Bean.Attribute.ChangeAttribute;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.widget.MaxByteLengthEditText;

public class LeaveMessageActivity extends BaseBBXActivity implements OnClickListener,TextWatcher{
	@Bind(R.id.btn_invoice)Button btn_invoice;
	@Bind(R.id.et_message)MaxByteLengthEditText et_message;
	@Bind(R.id.layout_invoice)LinearLayout layout_invoice;
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_leave_message);
		super.onCreate(savedInstanceState);
		
	}
	@Override
	protected void initView() {
		setTitle(true,getString(R.string.title_message), -1, getString(R.string.title_right_confirm));
		btn_invoice.setOnClickListener(this);
		et_message.setMaxByteLength(60);
		et_message.addTextChangedListener(this);
		if(getIntent().getExtras()!=null){
			MainAttribute main=MainAttribute.getInstance();
			ChangeAttribute change=ChangeAttribute.getInstance();
			switch(getIntent().getExtras().getInt(MainMsg.extra_message)){
			case MainMsg.MESSAGE_MAIN_PC:
				if(main.getMessage_PC()!=null&&!main.getMessage_PC().equals("")){
					et_message.setText(main.getMessage_PC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_MAIN_BC:
				if(main.getMessage_PC()!=null&&!main.getMessage_PC().equals("")){
					et_message.setText(main.getMessage_PC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_MAIN_KJ:
				et_message.setHint("可以在此描述您的货品，以便为您安排更合适的车辆");
				layout_invoice.setVisibility(View.GONE);
				if(main.getMessage_KJ()!=null&&!main.getMessage_KJ().equals("")){
					et_message.setText(main.getMessage_KJ());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_CHANGE_PC:
				if(change.getMessage_PC()!=null&&!change.getMessage_PC().equals("")){
					et_message.setText(change.getMessage_PC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_CHANGE_BC:
				if(change.getMessage_BC()!=null&&!change.getMessage_BC().equals("")){
					et_message.setText(change.getMessage_BC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_CHANGE_KJ:
				layout_invoice.setVisibility(View.GONE);
				if(main.getMessage_KJ()!=null&&!main.getMessage_KJ().equals("")){
					et_message.setText(main.getMessage_KJ());
					et_message.setSelection(et_message.length());
				}
			}
		}
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tv_right:
			String message=et_message.getText().toString().toString();
			Bundle bundle = new Bundle();
			bundle.putString("message",message);
			Intent intent = new Intent();
			intent.putExtras(bundle);
			setResult(resultCode, intent);
			onfinish();	
			break;
		case R.id.btn_invoice:
			et_message.append(btn_invoice.getText()+";");
			setEnabled(btn_invoice,false);
			break;
		    default:
		    	super.onClick(v);
		    	break;
		}
	}
    public void setEnabled(Button v,boolean enabled){
    	if(enabled){
    		v.setEnabled(enabled);
    		v.setTextColor(getResources().getColor(R.color.text_nor));
    	}
    	else{
    		v.setEnabled(enabled);
    		v.setTextColor(getResources().getColor(R.color.white));
    	}
    	
    }
	@Override
	public void afterTextChanged(Editable s) {
		if(!s.toString().contains(getString(R.string.text_need_invoice)+";")){
			setEnabled(btn_invoice,true);
		}
		
	}
	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}
}
